<?php
//
//	file: includes/class_cash_view.php
//	begin: 07/12/2006
//	$Author: christopher $
//	$Revision: 14 $
//	$Date: 2007-10-08 14:55:03 +0000 (Mon, 08 Oct 2007) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: class for displaying cash info in various places (profile, viewtopic, etc)

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
	exit;
}

class cash_events extends cache
{
	var $events;
	function cash_events()
	{
		global $db;
		$this->events = array();
		parent::cache('dta_cm_events', $config->data['cache_path'], $config->data['cache_disabled_cmevent']);
		$this->read();
		foreach($this->data as $row)
		{
			$this->events[$row['event_name']] = $row['event_data'];
		}
	}
	function read($force=false)
	{
		$ex_sql = "SELECT * FROM " . CASH_EVENTS_TABLE;
		$this->data = $this->sql_query($ex_sql, __LINE__, __FILE__, true);
	}
	function get_event_data($string)
	{
		global $board_config;
		if ( $board_config['cash_disable'])
		{
			return array();
		}
		if ( isset($this->events[$string]) )
		{
			return cash_event_unpack($this->events[$string]);
		}
		else
		{
			return array();
		}
	}
}
$cm_events = new cash_events();

class cash_memberlist
{
	function droplists(&$mode_types_text,&$mode_types)
	{
		global $board_config, $cash;
		if ( $board_config['cash_disable'])
		{
			return;
		}
		while ( $c_cur = &$cash->currency_next($cm_i,CURRENCY_ENABLED | CURRENCY_VIEWMEMBERLIST) )
		{
			$mode_types_text[] = $c_cur->name(true);
			$mode_types[] = 'cash_' . $c_cur->id();
		}
	}
	function modecheck($mode)
	{
		global $board_config, $cash;
		if ( $board_config['cash_disable'])
		{
			return 'cash_mod';
		}
		while ( $c_cur = &$cash->currency_next($cm_i,CURRENCY_ENABLED | CURRENCY_VIEWMEMBERLIST) )
		{
			if ( $mode == 'cash_' . $c_cur->id() )
			{
				return $mode;
			}
		}
		return 'cash_mod';
	}
	function getfield($mode)
	{
		global $cash;
		$id = substr($mode,5);
		$c_cur = &$cash->currency($id);
		return $c_cur->db();
	}
	function generate_columns(&$sql,$num_columns = 8)
	{
		global $board_config, $cash;
		global $template;
		if ( $board_config['cash_disable'] )
		{
			$template->assign_vars(array(
				'NUM_COLUMNS' => $num_columns,
			));
			return;
		}
		// whee! now that we have the $template, we can do whatever we want with it! yay!
		$cash_field = "";
		$count = $cash->currency_count(CURRENCY_ENABLED | CURRENCY_VIEWMEMBERLIST);
		$template->assign_vars(array('NUM_COLUMNS' =>$count + $num_columns));
		while ( $c_cur = &$cash->currency_next($cm_i,CURRENCY_ENABLED | CURRENCY_VIEWMEMBERLIST) )
		{
			$template->assign_block_vars('cashrow',array('NAME' => $c_cur->name()));
			$cash_field .= $c_cur->db() . ', ';
		}
		if ( strstr($sql,'*') )
		{
			return;
		}
		$insertpoint = strpos($sql,'user_id');
		$sql = substr($sql,0,$insertpoint) . $cash_field . substr($sql,$insertpoint);
	}
	function listing(&$template,&$row)
	{
		global $board_config, $cash;
		if ( $board_config['cash_disable'])
		{
			return;
		}
		while ( $c_cur = &$cash->currency_next($cm_i,CURRENCY_ENABLED | CURRENCY_VIEWMEMBERLIST) )
		{
			$template->assign_block_vars('memberrow.cashrow', array('CASH_DISPLAY' => $c_cur->display($row[$c_cur->db()])));
		}
	}
}
$cm_memberlist = new cash_memberlist();

class cash_viewtopic
{
	var $template;
	function generate_columns($forum_id,&$sql)
	{
		global $board_config, $cash;
		if ( $board_config['cash_disable'])
		{
			return '';
		}

		if ( strstr($sql,'u.*') )
		{
			return;
		}
		$cash_field = "";
		while ( $c_cur = &$cash->currency_next($cm_i,CURRENCY_ENABLED | CURRENCY_VIEWTOPIC,$forum_id) )
		{
			$cash_field .= 'u.' . $c_cur->db() . ', ';
		}
		$insertpoint = strpos($sql,'u.user_id');
		$sql = substr($sql,0,$insertpoint) . $cash_field . substr($sql,$insertpoint);
	}
	function post_vars(&$postdata,&$userdata,$forum_id)
	{
		global $template, $user;
		global $board_config, $lang, $phpEx, $cash;
		if ( $board_config['cash_disable'])
		{
			return;
		}
		$mask = false;
		if ( $userdata['user_level'] != ADMIN )
		{
			$mask = CURRENCY_ENABLED;
			if ( $postdata['user_id'] != $userdata['user_id'] )
			{
				//$mask &= CURRENCY_VIEWTOPIC;
			}
		}
		else
		{
			$forum_id = false;
		}

		$template->assign_block_vars('postrow.cash', array());
		while ( $c_cur = &$cash->currency_next($cm_i,$mask,$forum_id) )
		{
			$template->assign_block_vars('postrow.cash.cashrow', array(	'CASH_DISPLAY' => $c_cur->display($postdata[$c_cur->db()])));
		}
		if ( ($cash->currency_count(CURRENCY_ENABLED | CURRENCY_EXCHANGEABLE,$forum_id) >= 2) && $userdata['session_logged_in'] )
		{
			$template->assign_block_vars('postrow.cash.cashlinks',array(	'U_LINK' => append_sid("cash.$phpEx"),
															'L_NAME' => $user->lang('Exchange')));
		}
		if ( $cash->currency_count(CURRENCY_ENABLED | CURRENCY_DONATE,$forum_id) && ($userdata['user_id'] != $postdata['user_id']) && $userdata['session_logged_in'] )
		{
			$template->assign_block_vars('postrow.cash.cashlinks',array(	'U_LINK' => append_sid('cash.'.$phpEx.'?mode=donate&amp;ref=viewtopic&'.POST_USERS_URL.'='.$postdata['user_id'].'&amp;'.POST_POST_URL.'='.$postdata['post_id']),
															'L_NAME' => $user->lang('Donate')));
		}
		if ( $cash->currency_count(false,$forum_id) && (($userdata['user_level'] == ADMIN) || (($userdata['user_level'] == MOD) && $cash->currency_count(CURRENCY_ENABLED | CURRENCY_MODEDIT | CURRENCY_VIEWTOPIC, $forum_id))) )
		{
			$template->assign_block_vars('postrow.cash.cashlinks',array(	'U_LINK' => append_sid('cash.'.$phpEx.'?mode=modedit&amp;ref=viewtopic&'.POST_USERS_URL.'='.$postdata['user_id'].'&amp;'.POST_POST_URL.'='.$postdata['post_id']),
															'L_NAME' => sprintf($user->lang('Mod_usercash'),$postdata['username'])));
		}
		$template->set_filenames(array('cashbox' => 'cash_viewtopic.tpl'));
		if ( method_exists($template, 'unset_block_vars') )
		{
			$template->assign_lastblock_vars('postrow', array(
				'CASH' => $template->get_pparse('cashbox'),
			));
			$template->unset_block_vars('postrow.cash');
		}
		else
		{
			$add_dot = defined('CH_extended_templates') ? '' : '.';
			$template->_tpldata['postrow' . $add_dot][ count($template->_tpldata['postrow' . $add_dot]) -1 ]['CASH'] = $template->get_pparse('cashbox');
			unset($template->_tpldata['postrow' . $add_dot][ count($template->_tpldata['postrow' . $add_dot]) -1 ]['cash' . $add_dot]);
		}
	}
}
$cm_viewtopic = new cash_viewtopic();


class cash_viewprofile
{
	function post_vars(&$profiledata,&$userdata)
	{
		global $board_config, $lang, $phpEx, $cash;
		if ( $board_config['cash_disable'])
		{
			return;
		}
		global $template, $user;
		$mask = false;
		if ( $userdata['user_level'] != ADMIN )
		{
			$mask = CURRENCY_ENABLED;
			if ( $profiledata['user_id'] != $userdata['user_id'] )
			{
				$mask &= CURRENCY_VIEWPROFILE;
			}
		}
		$template->set_filenames(array(
			'cm_viewprofile' => 'cash_viewprofile.tpl')
		);
		
		while ( $c_cur = &$cash->currency_next($cm_i,$mask) )
		{
			$template->assign_block_vars('cashrow', array(	'CASH_NAME' => $c_cur->name(),
															'CASH_AMOUNT' => $profiledata[$c_cur->db()]));
		}
		if ( $userdata['session_logged_in'] && (($cash->currency_count(CURRENCY_ENABLED | CURRENCY_EXCHANGEABLE) >= 2) || ($cash->currency_count(CURRENCY_ENABLED | CURRENCY_DONATE) && ($userdata['user_id'] != $profiledata['user_id'])) || ($cash->currency_count() && (($userdata['user_level'] == ADMIN) || (($userdata['user_level'] == MOD) && $cash->currency_count(CURRENCY_ENABLED | CURRENCY_MODEDIT))))) )
		{
			$template->assign_block_vars('switch_cashlinkson',array());
			if ( $cash->currency_count(CURRENCY_ENABLED | CURRENCY_EXCHANGEABLE) >= 2 )
			{
				$template->assign_block_vars('switch_cashlinkson.cashlinks',array(	'U_LINK' => append_sid("cash.$phpEx"),
																					'L_NAME' => $user->lang('Exchange')));
			}
			if ( $cash->currency_count(CURRENCY_ENABLED | CURRENCY_DONATE) && ($userdata['user_id'] != $profiledata['user_id']) )
			{
				$template->assign_block_vars('switch_cashlinkson.cashlinks',array(	'U_LINK' => append_sid('cash.'.$phpEx.'?mode=donate&amp;ref=viewprofile&amp;'.POST_USERS_URL.'='.$profiledata['user_id']),
																					'L_NAME' => $user->lang('Donate')));
			}
			if ( $cash->currency_count() && (($userdata['user_level'] == ADMIN) || (($userdata['user_level'] == MOD) && $cash->currency_count(CURRENCY_ENABLED | CURRENCY_MODEDIT))) )
			{
				$template->assign_block_vars('switch_cashlinkson.cashlinks',array(	'U_LINK' => append_sid('cash.'.$phpEx.'?mode=modedit&amp;ref=viewprofile&amp;'.POST_USERS_URL.'='.$profiledata['user_id']),
																					'L_NAME' => sprintf($user->lang('Mod_usercash'),$profiledata['username'])));
			}
		}
		$template->assign_vars(array(
			'CASH' => $template->get_pparse('cm_viewprofile')
		));
	}
}
$cm_viewprofile = new cash_viewprofile();

class cash_posting
{
	function update_post($mode, &$post_data, $forum_id, $topic_id, $post_id, $topic_type, $bbcode_uid, $post_username, &$post_message)
	{
		global $board_config, $userdata;
		if ( $board_config['cash_disable'] || (($mode != 'newtopic') && ($mode != 'reply') && ($mode != 'editpost')) )
		{
			return '';
		}
		$first_post = $post_data['first_post'];
		$poster_id = $userdata['user_id'];
		$old_message = '';
		$new_bbcode = $bbcode_uid;
		$old_bbcode = '';
		if ( $mode == 'editpost' )
		{
			$poster_id = $post_data['poster_id'];
			$old_message = &$post_data['post_text'];
			$old_bbcode = $post_data['bbcode_uid'];
		}
		if ( $mode == 'reply' )
		{
			$topic_starter = $post_data['topic_poster'];
		}
		else
		{
			$topic_starter = false;
		}
		return $this->cash_update($mode, $poster_id, $first_post, $old_message, $post_message, $forum_id, $topic_id, $post_id, $new_bbcode, $topic_starter, $old_bbcode);
	}
	function update_delete($mode, &$post_data, $forum_id, $topic_id, $post_id)
	{
		global $board_config;
		if ( $board_config['cash_disable'] || ($mode != 'delete') )
		{
			return;
		}
		$first_post = $post_data['first_post'];
		$poster_id = $post_data['poster_id'];
		$new_message = '';
		$new_bbcode = '';
		$old_bbcode = $post_data['bbcode_uid'];
		$topic_starter = ANONYMOUS;
		$this->cash_update($mode, $poster_id, $first_post, $post_data['post_text'], $new_message, $forum_id, $topic_id, $post_id, $new_bbcode, $topic_starter, $old_bbcode);
	}
	function cash_update($mode, $poster_id, $first_post, &$old_message, &$new_message, $forum_id, $topic_id, $post_id, $new_bbcode, $topic_starter, $old_bbcode)
	{
		global $board_config, $lang, $db, $phpbb_root_path, $phpEx, $userdata, $cash;

		if ( ($mode == 'reply') && ($poster_id != $topic_starter) && ($topic_userdata = get_userdata($topic_starter)) )
		{
			$topic_creator = new cash_user($topic_starter,$topic_userdata);
			$topic_creator->give_bonus($topic_id);
		}
		if ( $poster_id == ANONYMOUS )
		{
			return;
		}
		if ( $userdata['user_id'] == $poster_id )
		{
			$posting_user = new cash_user($userdata['user_id'], $userdata);
		}
		else
		{
			$posting_user = new cash_user($poster_id);
		}
		$all_active = true;
		$forumcount = array();
		$forumlist = array();
		if ( (($mode == 'newtopic') || ($mode == 'reply')) && (intval($board_config['cash_disable_spam_num']) > 0) )
		{
			$all_active = false;
			$interval = time() - (3600 * intval($board_config['cash_disable_spam_time']));
			$sum = 0;
			$sql = "SELECT forum_id, count(post_id) as postcount
				FROM " . POSTS_TABLE . "
				WHERE poster_id = $poster_id
					AND post_time > $interval
				GROUP BY forum_id";
			$db->sql_query($sql, true, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$forumlist[] = $row['forum_id'];
				$forumcount[$row['forum_id']] = $row['postcount'];
				$sum += $row['postcount'];
			}
			if ( $sum < $board_config['cash_disable_spam_num'] )
			{
				$all_active = true;
			}
		}
		$new_len = array(strlen($new_message),cash_quotematch($new_message,$new_bbcode));
		$old_len = array(strlen($old_message),cash_quotematch($old_message,$old_bbcode));
		$sql_clause = array();
		$message_clause = array();
		$reply_bonus = array();
		$all_spam = !$all_active;
		while ( $c_cur = &$cash->currency_next($cm_i,CURRENCY_ENABLED,$forum_id) )
		{
			$this_enabled = $all_active;
			if ( !$all_active )
			{
				$sum = 0;
				for ( $i = 0; $i < count($forumlist); $i++ )
				{
					if ( $c_cur->forum_active($forumlist[$i]) )
					{
						$sum += $forumcount[$forumlist[$i]];
					}
				}
				if ( $sum < $board_config['cash_disable_spam_num'] )
				{
					$this_enabled = true;
					$all_spam = false;
				}
			}
			if ( $this_enabled )
			{
				$base = ( $first_post ) ? $posting_user->get_setting($c_cur->id(),'cash_perpost') : $posting_user->get_setting($c_cur->id(),'cash_perreply');
				$perchar = $posting_user->get_setting($c_cur->id(),'cash_perchar',PERCHAR_DEC_BONUS);
				$max = $posting_user->get_setting($c_cur->id(),'cash_maxearn');
				$quotes = ( $c_cur->mask(CURRENCY_INCLUDEQUOTES) ) ? 0 : 1;

				$total_added = ( $mode != 'delete' ) ? min($max,$base + ($perchar * $new_len[$quotes])) : 0;
				$total_removed = ( ($mode != 'newtopic') && ($mode != 'reply') ) ? min($max,$base + ($perchar * $old_len[$quotes])) : 0;
				$total_change = $total_added - $total_removed;
				if ( $total_change != 0 )
				{
					$change_sign = ($total_change > 0);
					$change_amount = ( ( $change_sign ) ? $total_change : (-$total_change) );
					$change_sign = ( ( $change_sign ) ? " + " : " - " );
					$sql_clause[] = $c_cur->db() . " = " . $c_cur->db() . $change_sign . $change_amount;
					$message_clause[] = $c_cur->display($change_amount);
				}
			}
		}
		if ( $all_spam )
		{
			return $board_config['cash_disable_spam_message'];
		}
		if ( count($sql_clause) > 0 )
		{
			$sql = "UPDATE " . USERS_TABLE . " 
				SET " . implode(', ',$sql_clause) . "
				WHERE user_id = " . $poster_id;
			$db->sql_query($sql, true, __LINE__, __FILE__);
		}
		return ( ($userdata['user_id'] == $poster_id) && ($board_config['cash_display_after_posts'] == 1) ) ? sprintf($board_config['cash_post_message'],implode(', ',$message_clause)) : '';
	}
}
$cm_posting = new cash_posting();

?>